﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emgu.CV;
using Emgu.CV.Structure;

/**
 * 视频管理类
 * @author: gr
 * @date:   2014-07-06
 * @func:   进行视频的管理
 * */

namespace CropPic
{
    /**
     * 视频管理类
     * */
    class VideoManager
    {
        private string videoName;
        private int framePoint = 0;
        //视频总帧数
        private double totalFrames = 0;
        //步长
        private int frameStep = 1;
        

        //以下变量没有使用
        //用来设置共需要多少帧
        private const int NEEDEDFRAME = 60;
       

        public string VideoName
        {
            set { videoName = value; }
            get { return videoName; }
        }
        
        public int FramePoint
        {
            get { return framePoint; }
            set { framePoint = value; }
        }

        public double TotalFrames
        {
            get { return totalFrames; }
        }

        public int FrameStep
        {
            set { frameStep = value; }
            get { return frameStep; }
        }

        private Capture capture;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_videoName">视频的全路径</param>
        public VideoManager(string _videoName)
        {
            VideoName = _videoName;
            capture = new Capture(_videoName);
            totalFrames = capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_COUNT);
        }

        
        /// <summary>
        /// 获取当前帧图片
        /// </summary>
        /// <returns>Image<Bgr, byte>: 当前帧</returns>
        public Image<Bgr, byte> getFrame()
        {
            capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES, framePoint);
            return capture.QueryFrame();
        }

        /**
        * filePoint指向下一个文件
        * */
        public bool toNext()
        {
            if (framePoint + frameStep < totalFrames)
            {
                framePoint += frameStep;
                return true;
            }
            else
            {
                return false;
            }
        }


        /**
         * filePoint指向上一个文件
         * */
        public bool toPrev()
        {
            if (framePoint - frameStep > 0)
            {
                framePoint -= frameStep;
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 判断视频是否读完
        /// </summary>
        /// <returns>true: 最后一帧，false:不是</returns>
        public bool isFinished()
        {
            return framePoint >= totalFrames;
        }

    }
}
